//index.js
//获取应用实例
var app = getApp()
Page({
    data: {
        controls: true,
        // danmuBtn:false,
        // danmuList:[],
        enableDanmu: false,
        autoPlay: false,
        // poster: "../img/me01.png",

        scrollindex: 0, //当前页面的索引值
        totalnum: 0, //总共页面数
        starty: 0, //开始的位置x
        endy: 0, //结束的位置y
        critical: 100, //触发翻页的临界值
        margintop: 0, //滑动下拉距离
        indexShow: 0,

        bgcolor: '#fff100',
        guanzhu: true,
        acount: 0,
        txt: [{ 0: '这是一个无情的夜，下着有些伤心的雨。' }, { 0: '这是一个无情的夜，下着有些伤心的雨。' }],
        show1: '',
        pick:false,
        index:0
    },


    // getUserInfo() {
    //     if (this.data.userInfo) return
    //     let that = this
    //     wx.openSetting({
    //         success(res) {
    //             if (res.authSetting['scope.userInfo']) {
    //                 wx.getUserInfo({
    //                     success(data) {
    //                         // console.log(data)
    //                         wx.setStorageSync('userInfo', data.userInfo)
    //                         that.setData({
    //                             userInfo: data.userInfo,
    //                             logins: true
    //                         })
    //                         app.wxlogin(that.getMyInfo)
    //                     }
    //                 })
    //             }
    //         }
    //     })
    // },
    //事件处理函数
    onLoad: function(options) {


        var that = this
        var session_key = wx.getStorageSync('session_key');


        app.wxlogin(that.ss)

        wx.request({
                url: app.data.baseUrl + '/api/Index/index',
                data: {
                    session_key: session_key
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: function(res) {
                    console.log('res', res)
                    var userLists = res.data.data;
                    for (var i = 0; i < userLists.length; i++) {
                        if (!userLists[i].no) {
                            userLists[i].ys = 1;
                            userLists[i].auter = false;
                        }
                        // console.log(userLists[i].video_url)
                        // var inde = userLists[i].video_url.indexOf('.');
                        // console.log(inde)
                        // var type = userLists[i].video_url.substr(36, 5);

                        // // console.log(type)
                        // // console.log('aaaa', userLists[i].video_url)
                        // if (type == 'image') {
                        //     userLists[i].image_url = userLists[i].video_url
                        //         // console.log('bbb', userLists[i].image_url)
                        // }
                        // // console.log('ddd', userLists[i].image_url)
                        // console.log()
                    }

                    that.setData({
                        userLists: userLists
                    })

                }
            })
            //调用应用实例的方法获取全局数据
        app.getUserInfo(function(userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            })
        })

    },
    ss: function() {
        var that = this;
        var session_key = wx.getStorageSync('session_key')
        wx.request({
            url: app.data.baseUrl + '/api/Index/index',
            data: {
                session_key: session_key
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                console.log(res)
                var userLists = res.data.data;
                for (var i = 0; i < userLists.length; i++) {
                    if (!userLists[i].no) {
                        userLists[i].ys = 1;
                        userLists[i].auter = false;
                    }
                }
                that.setData({
                    userLists: userLists
                })

            }
        })
    },
    onShow: function() {

    },
    onLaunch: function() {

    },

    scrollTouchstart: function(e) {
        let py = e.touches[0].pageY;
        console.log(py)
        this.setData({
            starty: py
        })
    },
    // scrollTouchmove: function(e) {
    //     let py = e.touches[0].pageY;
    //     let d = this.data;
    //     this.setData({
    //         endy: py,
    //     })
    //     if (py - d.starty < 100 && py - d.starty > -100) {
    //         this.setData({
    //             margintop: py - d.starty
    //         })
    //     }
    // },
    
    // scrollTouchend: function(e) {
    //     let d = this.data;
    //     if (d.endy - d.starty > 100 && d.scrollindex > 0) {
    //         this.setData({
    //             scrollindex: d.scrollindex - 1
    //         })
    //     } else if (d.endy - d.starty < -100 && d.scrollindex < this.data.totalnum - 1) {
    //         this.setData({
    //             scrollindex: d.scrollindex + 1
    //         })
    //     }
    //     this.setData({
    //         starty: 0,
    //         endy: 0,
    //         margintop: 0
    //     })
    // },

    //评论
    bindval: function(e) {
        console.log('eeeeeeeeee', e)
        var that = this;
        var val = e.detail.value;
        var vals = val.substr(val.length - 1, 1);
        console.log(vals)
        var session_key = wx.getStorageSync('session_key');
        if(!that.data.val){
          that.setData({
            full_name:''
          })
        }
        if (vals == '@') {
            // console.log('aaa@@@@');
           if(that.data.full_name){
             return
           }
            wx.request({
                url: app.data.baseUrl + '/api/Index/userName',
                data: {
                    session_key: session_key,
                    v_id: that.data.v_id
                },
                success: function(res) {
                    console.log(res)
                    var names = res.data.data;
                    var user_nicenames = [];
                    for (var i = 0; i < names.length; i++) {
                        user_nicenames[i] = names[i].user_nicename;
                        names[i].auter = false
                    }
                    that.setData({
                        names: res.data.data,
                        user_nicenames: user_nicenames,
                        pick: true
                    })

                }
            })
        }
        that.setData({
          val:e.detail.value
        })

    },
    bindPickerChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        console.log(e)
        this.setData({
            index: e.detail.value
        })
    },
    //评论---评论框控制
    catchpinglun: function(e) {
      var that = this;
      var session_key = wx.getStorageSync('session_key');
      var that = this;
      var v_uid = e.currentTarget.id;
      var v_id = e.currentTarget.dataset.v_id;
      var ys = e.currentTarget.dataset.ys;
      var auter = e.currentTarget.dataset.auter;
      var index = e.currentTarget.dataset.index;
      var userLists = that.data.userLists;
      var to_uid = that.data.to_uid;
      that.setData({
        v_uid: v_uid,
        to_uid: to_uid,
        v_id: e.currentTarget.dataset.v_id
      })
      wx.request({
        url: app.data.baseUrl + '/api/Index/comments',
        data:{
          v_id: e.currentTarget.dataset.v_id,
          session_key: session_key
        },
        success(res){
          console.log('qqqqqqqqqqq',res);
          that.setData({
            content:res.data.data
          })
        }
      })
        console.log(e)
       


        // if (ys == 1) {
        //     userLists[index].ys = 0
        //     console.log("aaa")
        // } else {
        //     console.log("bbb")
        //     userLists[index].ys = 1
        // }
        console.log('000000iiiipp')
        that.setData({
            userLists: userLists,
            show1: 'active',
            // id:id,
            v_id:v_id
        })
    },
    catchpinglun1(){
      var that = this;
      var session_key = wx.getStorageSync('session_key');
      console.log(session_key);
      if(!that.data.val){
        wx.showToast({
          title: '评论不能为空',
        })
        return
      }
      wx.request({
        url: 'http://sheying.j8j0.com/api/Index/comment',
        method:'POST',
        header:{
            'content-type':'application/x-www-form-urlencoded'
        },
        data:{
          session_key: session_key,
          v_id:that.data.v_id,
          v_uid:that.data.v_uid,
          content:that.data.val,
          to_uid:that.data.to_uid,
          full_name:that.data.full_name
        },
        success(res){
          console.log(res)
          that.setData({
            show1:'',
            val:'',
            full_name:'',
            to_uid:'',
            v_id:'',
            v_uid:''
          })
        }
      })
    },
    bindTijiao: function(e) {
        var that = this;
        var session_key = wx.getStorageSync('session_key')
        wx.request({
            url: app.data.baseUrl + '/api/Index/comment',
            data: {
                content: that.data.val,
                session_key: session_key,
                v_id: e.currentTarget.id,
                v_uid: e.currentTarget.dataset.v_id
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                console.log(res)
            }
        })
        that.setData({
            Bl: false
        })
    },
    //关注
    bindguanzhu: function(e) {
        var that = this;
        console.log(e)
        var g_uid = e.currentTarget.id;
        var session_key = wx.getStorageSync('session_key');
        var index = e.currentTarget.dataset.index;
        var userLists = that.data.userLists;
        var guanzhu = userLists[index].guanzhu;
        wx.request({
            url: app.data.baseUrl + '/api/Index/concern',
            data: {
                session_key: session_key,
                g_uid: g_uid
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                console.log(res)
                if (guanzhu == 0) {
                    userLists[index].guanzhu = 1
                }
                if (guanzhu == 1) {
                    userLists[index].guanzhu = 0
                }

                that.setData({
                    userLists: userLists
                })
            }
        })
    }, //赞
    bindZan: function(e) {
        console.log(e)
        var that = this;
        var session_key = wx.getStorageSync('session_key');
        var index = e.currentTarget.dataset.index;
        var userLists = that.data.userLists;
        var mind = userLists[index].mind;
        wx.request({
            url: app.data.baseUrl + '/api/Index/mind',
            data: {
                session_key: session_key,
                v_id: e.currentTarget.dataset.v_id,
                v_uid: e.currentTarget.id
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                console.log(res)
                if (mind == 0) {
                    userLists[index].mind = 1
                }
                if (mind == 1) {
                    userLists[index].mind = 0
                }

                that.setData({
                    userLists: userLists
                })
            }
        })
    },
    //收藏
    bindShoucang: function(e) {
        var that = this;
        var session_key = wx.getStorageSync('session_key');
        console.log(e)
        var index = e.currentTarget.dataset.index;
        var userLists = that.data.userLists;
        var favorite = userLists[index].favorite;
        wx.request({
            url: app.data.baseUrl + '/api/Index/favorite',
            data: {
                session_key: session_key,
                v_id: e.currentTarget.dataset.v_id,
                v_uid: e.currentTarget.id
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                console.log(res)
                if (favorite == 0) {
                    userLists[index].favorite = 1
                }
                if (favorite == 1) {
                    userLists[index].favorite = 0
                }

                that.setData({
                    userLists: userLists
                })
            }
        })
    },
    //浏览播放数
    bindacount: function(e) {
        console.log(e)
        var that = this;
        var acount = that.data.acount;
        ++acount;
        console.log(acount)

        var session_key = wx.getStorageSync('session_key')
        wx.request({
            url: app.data.baseUrl + '/api/Index/add_videoNum',
            data: {
                session_key: session_key,
                v_uid: e.currentTarget.id,
                v_id: e.currentTarget.dataset.id
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                console.log(res)
            }
        })
        that.setData({
            acount: acount
        })
    },




    // scrollTouchstart: function(e) {
    //     let py = e.touches[0].pageY;
    //     this.setData({
    //         starty: py
    //     })
    // },
    // scrollTouchmove: function(e) {
    //     let py = e.touches[0].pageY;
    //     let d = this.data;
    //     this.setData({
    //         endy: py,
    //     })
    //     if (py - d.starty < 100 && py - d.starty > -100) {
    //         this.setData({
    //             margintop: py - d.starty
    //         })
    //     }
    // },
    // scrollTouchend: function(e) {
    //     let d = this.data;
    //     if (d.endy - d.starty > 100 && d.scrollindex > 0) {
    //         this.setData({
    //             scrollindex: d.scrollindex - 1
    //         })
    //     } else if (d.endy - d.starty < -100 && d.scrollindex < this.data.totalnum - 1) {
    //         this.setData({
    //             scrollindex: d.scrollindex + 1
    //         })
    //     }
    //     this.setData({
    //         starty: 0,
    //         endy: 0,
    //         margintop: 0
    //     })
    // },
    bigImage: function(e) {
        console.log(e);
        var that = this;
        var imgindex = e.currentTarget.dataset.imgindex;
        var pindex = e.currentTarget.dataset.pindex;
        wx.previewImage({
            current: that.data.userLists[pindex].video_url[imgindex],
            urls: that.data.userLists[pindex].video_url
        })
    },
    bindother(e) {
      console.log(e)
      wx.navigateTo({
        url: '../me/otherPerson/otherPerson?uid=' + e.currentTarget.dataset.uid
      })
    },
    bindPickerChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      var that = this;
      var user_nicenames = that.data.user_nicenames;
      that.setData({
        index: e.detail.value,
        val: that.data.val + user_nicenames[e.detail.value] + ' ',
        pick:false,
        full_name: user_nicenames[e.detail.value],
        to_uid: that.data.names[e.detail.value].id
      })
    },
    xiaodiao(){
      this.setData({
        show1: '',
        val: '',
        full_name: '',
        to_uid: '',
        v_id: '',
        v_uid: '',
        pick:false
      })
    }
})